﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ADShop.Models;
using ADShop.Repository;
namespace ADShop.Controllers
{
    public class CheckoutController : Controller
    {
        //
        // GET: /Checkout/
        #region "Variable"
        //ADShopEntities adshop = new ADShopEntities();
        EShopEntities1 adshop = new EShopEntities1();
        #endregion
        public ActionResult Index()
        {
            if(Session["Member"] == null)
            {
                return RedirectToAction("LoginMember","Login");
            }
            ViewBag.PaymentsMethod = new SelectList(adshop.PaymentMethod.ToList(), "IDPaymentMethod", "NamePaymentMethod", 0);
            ViewBag.ShipMethod = new SelectList(adshop.ShipMethod.ToList(), "IDShipMethod", "NameShipMethod", 0);
            return View();
        }

        public ActionResult ConfirmSuccess()
        {
            return View();
        }

        public ActionResult ConfirmOrder(int id, FormCollection fc)
        {
            //Trước khi add vào Order . Thì phải add Vào chi tiết trước .
            //Sau đó lấy ID Order add qua Chi tiết
            FirstOrderProduct _firstOrderProduct = new FirstOrderProduct();
            _firstOrderProduct.IDUsers = id;
            _firstOrderProduct.TotalPriceOrder = (Session["Cart"] as ShoppingCart).GetTotal();
            _firstOrderProduct.Active = true;
            adshop.FirstOrderProduct.Add(_firstOrderProduct);
            adshop.SaveChanges();

            int IDOrderProduct = adshop.FirstOrderProduct.Max(d => d.IDOrderProduct);

            OrderProduct order = null;
            foreach (var items in ((ShoppingCart)Session["Cart"]).ListShoppingCartItems)
            {
                order = new OrderProduct();
                order.IDShipMethod = Convert.ToInt32(fc["dllShipMethod"]);
                order.IDOrderProduct = IDOrderProduct;
                order.IDPaymentMethod = Convert.ToInt32(fc["ddlPaymentsMethod"]);
                order.IDUser = id;
                order.Recipient = fc["tbNguoiNhan"];
                order.AddressRecipient = fc["tbDiaChi"];
                order.EmailRecipient = fc["tbEmail"];
                order.PhoneRecipient = fc["tbSoDienThoai"];
                order.Messenger = fc["tbTinNhan"];
                order.Active = true;
                order.IDProduct = items.ProductID;
                order.Quantity = items.Quantity.ToString();
                order.TotalPrice = Convert.ToInt64(items.TotalPrice);
                Global._orderProduct = order;
                adshop.OrderProduct.Add(order);
            }
            adshop.SaveChanges();
            return RedirectToAction("ConfirmSuccess");
        }

        public string GetPaymentName(int id)
        {
            return string.Format("<b>{0}</b>", adshop.PaymentMethod.SingleOrDefault(d => d.IDPaymentMethod == id).NamePaymentMethod);
        }

        public string GetShipName(int id)
        {
            return string.Format("<b>{0}</b>", adshop.ShipMethod.SingleOrDefault(d => d.IDShipMethod == id).NameShipMethod);
        }
        public ActionResult End()
        {
            Session["Cart"] = null;
            Global._orderProduct = null;
            return RedirectToAction("Index", "Default");
        }
    }
}
